package org.springframework.samples.mvc.basic.model;

import java.lang.reflect.Method;
import java.util.List;

import org.springframework.util.ReflectionUtils;

public class ReflectionTableModel<T extends Identifiable> extends
		ReflectionModel<T> implements UITableModel {

	private List<T> objects;

	public ReflectionTableModel(Class<T> type, List<T> objects,
			String... excludedFields) {
		super(type, excludedFields);
		this.objects = objects;
	}

	public Object getValueAt(int row, int column) {
		Method method = ReflectionUtils.findMethod(getType(), "get"
				+ capitalizeFirsLetter(getFieldNames()[column]));
		return ReflectionUtils.invokeMethod(method, objects.get(row));
	}

	private String capitalizeFirsLetter(String str) {
		return str.substring(0, 1).toUpperCase() + str.substring(1);
	}

	public int getNumberOfRows() {
		return objects.size();
	}
}
